﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConcordWinManager
{
    public partial class DoverennostEditForm : Form
    {
        public Consultant consultant;

        private string mode;

        private Form parentForm;

        bool act;

        public DoverennostEditForm()
        {
            InitializeComponent();
        }

        public DoverennostEditForm(Form parentForm, string mode)
        {
            InitializeComponent();

            this.mode = mode;

            this.Text = string.Format("Доверенность [{0}]", mode);

            this.parentForm = parentForm;

            if (this.mode == "Добавление")
            {
                //consultant = new Consultant();
                dateTimePickerBegin.Value = DateTime.Now;
                dateTimePickerEnd.Value = DateTime.Now;
                this.act = true;
            }
        }

        public DoverennostEditForm(Form parentForm, string mode, int id, ref Consultant consultant)
        {
            InitializeComponent();

            this.parentForm = parentForm;

            this.Text = string.Format("Консультант [{0}]", mode);

            this.mode = mode;

            if (mode == "Изменение")
            {
                this.consultant = consultant;
                Tools.RunWithProgressIndicator(this, delegate
                {
                    this.consultant.doverennost.SelectById(id);

                    if (this.consultant.doverennost.id_doverennost != 0)
                    {
                        tbxNumDoverennost.Text = this.consultant.doverennost.Num_doverennost.ToString();
                        dateTimePickerBegin.Value = this.consultant.doverennost.DateBegin;
                        dateTimePickerEnd.Value = this.consultant.doverennost.DateEnd;
                    }                    
                });

            }

            this.act = true;
        }

        private void DoverennostEditForm_Load(object sender, EventArgs e)
        {
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool IsFormValid()
        {
            if (tbxNumDoverennost.Text == string.Empty)
            {
                MessageBox.Show("Введите номер доверенности", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else if (dateTimePickerEnd.Value <= dateTimePickerBegin.Value)
            {
                MessageBox.Show("Дата начала должна быть меньше даты окончания", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool bSuccessfully = false;

            try
            {
                if (IsFormValid())
                {
                    consultant.doverennost.DateBegin = dateTimePickerBegin.Value;
                    consultant.doverennost.DateEnd = dateTimePickerEnd.Value;
                    consultant.doverennost.Num_doverennost = tbxNumDoverennost.Text.Trim().ToString();
                    consultant.doverennost.id_consultant = consultant.id_consultant;

                    if (this.mode == "Изменение")
                    {
                        Tools.RunWithProgressIndicator(this, delegate { consultant.doverennost.Update(); });

                        MessageBox.Show("Доверенность изменена успешно", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                    else
                    {
                        if (mode == "Добавление")
                        {
                            if (consultant.id_consultant != 0)
                            {
                                Tools.RunWithProgressIndicator(this, delegate { consultant.doverennost.Insert(); });

                                MessageBox.Show("Доверенность добавлена успешно", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                MessageBox.Show("Сначала создайте консультанта", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }
                    }
                    bSuccessfully = true;
                }
            }
            catch
            {
                ;
            }

            if (bSuccessfully)
            {
                this.Close();
            }
        }
    }
}
